﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace Nirvana
{
    /// <summary>
    /// 根据不同参数，修改animator初始参数
    /// </summary>
    public class AnimatorParameterGroup : MonoBehaviour
    {
        [Serializable]
        private struct AnimatorParameter
        {
            [SerializeField]
            public string Name;
            [SerializeField]
            public int ID;
            [SerializeField]
            public AnimatorControllerParameterType ParameterType;
            [SerializeField]
            public bool BooleanValue;
            [SerializeField]
            public int IntegerValue;
            [SerializeField]
            public float FloatValue;

        }
        [SerializeField]
        private Animator animator;
        [SerializeField]
        private bool activeOnInitialize = true;
        [SerializeField]
        private AnimatorParameter[] parameters;

        void Start()
        {
            if(this.activeOnInitialize && this.animator != null)
            {
                if (this.animator.isInitialized)
                    this.SetParameters();
                else
                    StartCoroutine(WaitInit());
            }
        }

        private IEnumerator WaitInit()
        {
            while (!this.animator.isInitialized) yield return null;
            this.SetParameters();
        }

        public  Animator GetAnimator()
        {
            return this.animator;
        }

        public void SetParameters()
        {
            if (this.animator == null) return;
            foreach(var p in this.parameters)
            {
                var _type = p.ParameterType;
                switch (_type)
                {
                    case AnimatorControllerParameterType.Float:
                        this.animator.SetFloat(p.ID, p.FloatValue);
                        break;
                    case AnimatorControllerParameterType.Int:
                        this.animator.SetInteger(p.ID, p.IntegerValue);
                        break;
                    case AnimatorControllerParameterType.Bool:
                        this.animator.SetBool(p.ID, p.BooleanValue);
                        break;
                    case AnimatorControllerParameterType.Trigger:
                        if (p.BooleanValue) this.animator.SetTrigger(p.ID);
                        else this.animator.ResetTrigger(p.ID);
                        break;
                }
            }

        }


    }
}

